package com.zmm.weblog.admin.utils;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/**
 * @Author: 福禄寿
 * @Date: 2024/5/31 上午10:52
 * @Des: weblog-springboot
 **/
public class threadTest {

    static Object lock = new Object();

    int ticketNum = 10;

    public static void main(String[] args) throws InterruptedException {

        threadTest ticket = new threadTest();
        for (int i = 0; i < 20; i++) {

            new Thread(ticket::getTicket).start();
        }
    }

    public void getTicket() {
        synchronized (lock) {
            if (ticketNum <= 0) {
                return;
            }
            System.out.println(Thread.currentThread().getName() + "抢到1张票,剩余：" + ticketNum);

            ticketNum--;
        }
    }
}


